<div class="content text">
<h1>新增商品</h1>

<% form_for([:admin , @product] , :html => { :multipart => true}) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :serial %><br />
    <%= f.text_field :serial %>
  </p>
  <p>
    <%= f.label :proved_serial %><br />
    <%= f.text_field :proved_serial %>
  </p>
  <p>
    <%= f.label :sort_id %><br />
    <%= f.select :sort_id ,Sort.all.map{|s| [s.name,s.id]}  %>
  </p>
  <p>
    <%= f.label :color_id %><br />
    <%= f.select :color_id , Color.all.map{|c| [c.name ,c.id]} %>
  </p>
  <p>
    <%= f.label :state %><br />
    <%= f.select :state , Product::STATE.map { |k,v|[v ,k]  } %>
  </p>
  <p>
    <%= f.label :rank %><br />
    <%= f.select :rank , Product::RANK.map { |k,v|[v ,k]  } %>
  </p>
  <p>
    <%= f.label :price %><br />
    <%= f.text_field :price %>
  </p>
  <p>
    <%= f.label :current_price %><br />
    <%= f.text_field :current_price %>
  </p>
  <p>
    <%= f.label :introduction %><br />
    <%= f.text_area :introduction %>
  </p>
  <p>
    <label for="product_picture_id">选择图像</label>
    <%=  file_field_tag(:image) %>
    <% unless @product.product_images.blank? %>
      <% @product.product_images.each do |picture| %>
      <div class="file_list" id="picture_< %= picture.id %>">
        <ul>
          <li>
            文件: <%= picture.image_file_name %>
            <%= link_to_remote "删除图片",:url=>delete_product_picture_ajax_path(:p_id=>picture.id),:confirm=>"是否要删除该产品图片?",:method=>:get%>
          </li>
        </ul>
      </div>
    <% end %>
  <% end %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', products_path %>

<script type="text/javascript">
  var multiple_upload_attachment_counter = 0;
  multiple_upload_attachment($("image"), 100);
  function multiple_upload_attachment(ele,max_size)
  {
    Event.observe(ele,'change',function(){
      addAttachmentDiv(ele,max_size);
    }
  );
    if(multiple_upload_attachment_counter>=max_size)ele.disabled=true;
  }
  function addAttachmentDiv(ele, max_size)
  {
    if(!$A(['jpg','bmp','png','gif','rar','zip','tar','gz','jar','war']).any(function(extName){return new RegExp('\\.'+extName+'$','i').test(ele.value);})){
      alert("文件类型错误！");
      return false;
    }
    multiple_upload_attachment_counter++;
    var div=new Element("div").update("<ul><li>文件: "+ele.value+" <a href='#' onclick='removeAttachmentDiv(this, \""+ele.id+"\");return false;'>删除</a></li></ul>");
    div.addClassName("file_list")
    var new_input=new Element("input",{type:"file",name:ele.name,id:ele.id,disabled:multiple_upload_attachment_counter>=max_size});
    Event.observe(new_input,'change',function(){
      addAttachmentDiv(new_input,max_size);
    }
  );
    ele.insert({before:new_input,after:div});
    ele.id=ele.id+multiple_upload_attachment_counter;
    ele.name="image[]";
    div.appendChild(ele.hide().remove());
  }
  function removeAttachmentDiv(link,eleId){
    multiple_upload_attachment_counter--;
    link.parentNode.parentNode.parentNode.remove();
    $(eleId).disabled=false;
  }
</script>    
</div>